﻿@using Huirui.Cavan.Domain.MainBoundedContext.SystemModule
@model Huirui.Cavan.Presentation.ViewModel.SystemModule.IBaseCodeRuleViewModel
@{Layout = null;}
@using (Html.BeginForm(ActionConst.ADD, ControllerConst.BASECODERULE))
{ 
    <div class="tool_bar">
        <input id="basecoderule_save" type="submit" style="display: none;" />
        <input id="basecoderule_reset" type="reset" style="display: none" />
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-save'" onclick="javascript:$('#basecoderule_save').click()">保存</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-undo'" onclick="javascript:$('#basecoderule_reset').click()">重置</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-collapse'" onclick="toggleHeard()">折叠/展开</a>
    </div>
    <table class="form_heard">
        <tr>
            <td class="c100">
                <label class="BlueRequired">单据类型</label>
            </td>
            <td class="c160">
                @Html.DropDownListFor(m => m.Item.Type, typeof(SystemIdentifier.BaseCodeRuleType), new { id = "base_type", defaultText = "请选择", defaultValue = "0" })
                @Html.HiddenFor(m => m.Item.Id, new { id = "baseCodeRuleId" })
            </td>
            <td class="c100">
                <label>规则名称</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.Name, new { id = "base_name" })
            </td>
            <td class="c100">
            </td>
            <td class="c160">
            </td>
        </tr>
        <tr>
            <td class="c100">
                <label>编码前缀:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.Prefix, new { id = "base_prefix" })
            </td>
            <td class="c100">
                <label>是否默认:</label>
            </td>
            <td class="c164">
                @Html.DropDownListFor(m => m.IsDefault, Model.IsDefaultOptions, new { id = "base_isdefault" })
            </td>
        </tr>
        <tr>
            <td class="c100">
                <label class="BlueRequired">流水号位数:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.SerialNumberNumber, new { id = "base_serialnumbernumber" })
            </td>
            <td class="c100">
                <label>起始流水号:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.SerialNumberStart, new { id = "base_serialnumberstart", disabled = "disabled" })
            </td>
            <td class="c100">
                <label>范例:</label>
            </td>
            <td class="c164">
                @Html.TextBoxFor(m => m.Item.Example, new { id = "base_example", disabled = "disabled" })
            </td>
        </tr>
    </table>
}
<table id="basecoderule">
</table>
<div id="basecoderuletb">
    @*    <a href="#" class="easyui-linkbutton" iconcls="icon-cut" plain="true" id="deleteBaseCodeRule" style="display:none">批量删除</a>*@ <a href="#" class="easyui-linkbutton" iconcls="icon-save" plain="true"
        onclick="javascript:alert('引出')">引出</a>
</div>
<script type="text/javascript">
    $(function ()
    {
        $("form[action^='/basecoderule/add']").submit(function ()
        {
            if ($("select#base_type").attr('value') == "0")
            {
                $.messager.alert("提示", "请选择要编辑的单据类型");
                return false;
            }
            var rows = $('#basecoderule').datagrid('getRows');
            var count = 0;
            for (var i = 0; i < rows.length; i++)
            {
                if ($("select#base_type").attr('value') == rows[i].Type.toString())
                {
                    count = count + 1;
                }
                if (count > 1)
                {
                    $.messager.alert("提示", "每个单据类型的编码规则只能创建一种");
                    return false;
                }
            }
            $("#base_example").attr("disabled", false);
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (data)
                {
                    if (data.IsSuccess)
                    {
                        loadData();
                    }
                    else
                    {
                        $.messager.alert("提示", data.Messages[0]);
                    }

                },
                error: function ()
                {
                    alert('系统繁忙，请稍候再试');
                }
            })
            $("#base_example").attr("disabled", true);
            return false;
        });

        $("select#base_type").bind("change", function ()
        {
            loadData();
        });

        function loadData()
        {
            $('#basecoderule').datagrid('loadData', { total: 0, rows: [] });
            $.ajax({
                url: '/basecoderule/list/' + $("select#base_type").val(),
                type: 'Post',
                success: function (result)
                {
                    if (result.total > 0)
                    {
                        $('#basecoderule').datagrid({
                            data: result
                        });
                    }
                },
                error: function ()
                {
                    alert('系统繁忙，请稍候再试');
                }
            })

            return false;
        }

        $('#basecoderule').datagrid({
            class: 'easyui-datagrid',
            title: "基础编码规则",
            singleSelect: true,
            fitColumns: true,
            toolbar: '#basecoderuletb',
            frozenColumns: [[{ field: 'ck', checkbox: true}]],
            columns: [[
                  { field: 'Name', title: '规则名称', width: 100 },
                  { field: 'Prefix', title: '编码前缀', width: 100 },
                  { field: 'SerialNumberNumber', title: '流水号数', width: 100 },
                  { field: 'SerialNumberStart', title: '起始流水号', width: 100 },
                  { field: 'IsDefault', title: '是否默认', width: 100 },
                  { field: 'Example', title: '示例', width: 150 }
            ]],
            onClickRow: function (rowIndex, rowData)
            {
                $("select#base_type").attr('value', rowData.Type);
                $("#baseCodeRuleId").val(rowData.Id);
                $("#base_name").val(rowData.Name);
                $("#base_prefix").val(rowData.Prefix);
                $("#base_serialnumbernumber").val(rowData.SerialNumberNumber);
                $("#base_serialnumberstart").val(rowData.SerialNumberStart);
                $("#base_example").val(rowData.Example);
                if (rowData.IsDefault == "是")
                {
                    $("select#base_isdefault").attr('value', '1');
                }
                else
                {
                    $("select#base_isdefault").attr('value', '0');
                }

            }
        });

        $("#base_prefix").bind("blur", function ()
        {
            getExample();
        });
        $("#base_serialnumbernumber").bind("blur", function ()
        {
            getExample();
        });
        $("#base_serialnumberstart").bind("blur", function ()
        {
            getExample();
        });

        //$("select#base_isdefault").bind("change", function () {
        //    if ($(this).val() == "1")
        //    {
        //        var rows = $('#basecoderule').datagrid('getRows');
        //        for (var i = 0; i < rows.length; i++) {
        //            if (rows[i].IsDefault == "是")
        //            {
        //                alert("只能有一个默认值");
        //                $("select#base_isdefault").attr('value', '0');
        //                return;
        //            }
        //        }
        //    }
        //});

        function getExample()
        {
            if ($("#base_serialnumbernumber").val() == "" || $("#base_serialnumberstart").val() == "")
            {
                return;
            }
            $.ajax({
                url: '/basecoderule/example?pf=' + $("#base_prefix").val() + "&snn=" + $("#base_serialnumbernumber").val() + "&sn=" + $("#base_serialnumberstart").val(),
                type: 'Post',
                success: function (result)
                {
                    if (result.IsSuccess)
                    {
                        $("#base_example").val(result.Messages[0]);
                    }
                },
                error: function ()
                {
                    alert('系统繁忙，请稍候再试');
                }
            })
        }

        //$("#deleteBaseCodeRule").bind("click", function () {
        //    if ($('#basecoderule').datagrid('getChecked').length) {
        //        var selected = $('#basecoderule').datagrid('getChecked');

        //        var effectRow = new Object();
        //        if (selected.length) {
        //            effectRow["selected"] = JSON.stringify(selected);
        //        }

        //        $.post("/basecoderule/delete", effectRow, function (data) {
        //            if (data.IsSuccess) {
        //                $('#basecoderule').datagrid('deleteChecked');
        //            }
        //        }, "JSON").error(function () {
        //            $.messager.alert("提示", "网络异常，请稍候再试");
        //        });
        //    }
        //});

        loadData();
    });


</script>
